package com.yk.serverhandler;

import com.yk.pojo.ReqMessage;
import com.yk.pool.ServerPool;
import com.yk.pool.command.MessageRevCommand;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * Created by dingkl on 2017/7/27.
 */
public class MessageRecvHandler extends ChannelInboundHandlerAdapter{
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        System.out.println("active");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("error");
        cause.printStackTrace();
        super.exceptionCaught(ctx, cause);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("read..");
        ReqMessage reqMessage = (ReqMessage) msg;
        System.out.println("sever:"+reqMessage.getMessageId()+",className:"+reqMessage.getClassName());
        ServerPool.getExecutorPool();
        ServerPool.submit(new MessageRevCommand(reqMessage,ctx));
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("inactive..");
        super.channelInactive(ctx);
    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
        System.out.println("WritabilityChanged..");
        super.channelWritabilityChanged(ctx);
    }
}
